﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADNLib;

namespace ADN
{
	public partial class FormShapeNav : Form
	{
        private Frame frame;

        public event EventHandler SelectionChanged;
        public event EventHandler ObjectNameChanged;

		public FormShapeNav()
		{
			InitializeComponent();
		}

        public FormShapeNav(Frame frame)
        {
            InitializeComponent();
        }

        public Frame WorkingFrame
        {
            get { return frame; }
            set 
            { 
                frame = value;
                LoadObjects();
            }
        }

        private void LoadObjects()
        {
            treeObjects.Nodes.Clear();

            if (frame != null)
            {
                for (int i = 0; i < frame.XObjects.Count; i++)
                {
                    CreateNodes(treeObjects.Nodes, frame.XObjects[i]);
                }
            }
        }

        private void CreateNodes(TreeNodeCollection nodes, XObject obj)
        {
            TreeNode child = new TreeNode();
            child.Text = obj.Name;

            nodes.Add(child);

            if (obj.Type == XObjectType.Group)
            {
                XGroup g = (XGroup)obj;
                for (int i = 0; i < g.Children.Count; i++)
                {
                    CreateNodes(child.Nodes, g.Children[i]);
                }
            }
        }

        private void OnSelectionChange()
        {
            ShapeNavEventArgs e = new ShapeNavEventArgs();
            if (SelectionChanged != null) { SelectionChanged(this, e); }
        }

        private void OnObjectNameChange(string name)
        {
            ShapeNavEventArgs e = new ShapeNavEventArgs();
            if (ObjectNameChanged != null) { ObjectNameChanged(this, e); }
        }

        private void treeObjects_AfterSelect(object sender, TreeViewEventArgs e)
        {
            OnSelectionChange();
        }

        private void treeObjects_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            OnObjectNameChange(e.Label);
        }
	}

    public class ShapeNavEventArgs : EventArgs
    {
        private string objName = String.Empty;
        private XObject affectedObj = null;
        private int index = 0;

        public string ObjectName 
        {
            get { return objName; }
            set { objName = value; }
        }
        
        public XObject AffectedObject 
        {
            get { return affectedObj; }
            set { affectedObj = value; }
        }
        public int Index 
        {
            get { return index; }
            set { index = value; }
        }
    }
}
